/*
 * WebCredentials.java
 *
 * Created on March 30, 2005, 10:33 PM
 */

package web;

import http.HTTPHelper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import xml.XMLHelper;

/**
 *
 * @author Keith
 */
public class WebCredentials {
  
  /**
   * Holds value of property loginPage.
   */
  final private URL loginPage;

  /**
   * Holds value of property userid.
   */
  final private String userid;

  /**
   * Holds value of property password.
   */
  final private String password;

  /**
   * Holds value of property responseGenerator.
   */
  final private Document responseGenerator;

  /** Creates a new instance of WebCredentials
   * @param loginPage
   * @param userid
   * @param password
   * @param responseGenerator 
   */
  public WebCredentials( URL loginPage, String userid, String password, Document responseGenerator ) {
    this.loginPage = loginPage;
    this.userid = userid;
    this.password = password;
    this.responseGenerator = responseGenerator;
  }
  
  /**
   * Getter for property loginPage.
   * @return Value of property loginPage.
   */
  public URL getLoginPage() {

    return this.loginPage;
  }

  /**
   * Getter for property login.
   * @return Value of property login.
   */
  public String getUserid()  {

      return this.userid;
  }

  /**
   * Getter for property password.
   * @return Value of property password.
   */
  public String getPassword() {

    return this.password;
  }

  /**
   * Getter for property responseGenerator.
   * @return Value of property responseGenerator.
   */
  public Document getResponseGenerator() {

    return this.responseGenerator;
  }

  /**
    * 1. get login page
    * 2. build authentication request
    *    a. get 'post' location
    *    b. incorporate credentials (login/password)
    * 3. post authentication request
    * 4. get authentication response
    *    a. cookies
   * @throws MalformedURLException
   * @throws URISyntaxException
   * @throws IOException 
   */
  public void authenticate() throws MalformedURLException, URISyntaxException, IOException {
    final Document loginDom = HTTPHelper.getPage( loginPage );
    
    try {
      final DOMResult domResult = new DOMResult();

//      XMLHelper.dump( loginDom );
      
      final Transformer transformer = XMLHelper.getTransformer( responseGenerator );

      transformer.setParameter( "userid", userid );
      transformer.setParameter( "password", password );
      
      transformer.transform( new DOMSource( loginDom ), domResult );
      
      final Document response = (Document)domResult.getNode();
//      XMLHelper.dump( response );

      final String path = XMLHelper.getNodeValue( response.getDocumentElement(), "path" );
      final String auth = XMLHelper.getNodeValue( response.getDocumentElement(), "post-content" );
      
      final Document result = HTTPHelper.getPage( new URL( loginPage, path ), auth );
//      XMLHelper.dump( result );
    } catch ( TransformerConfigurationException e ){
      e.printStackTrace();
    } catch ( TransformerException e ){
      e.printStackTrace();
    }
  }
}
